package com.LibreGeoSocial.Api;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;

import com.LibreGeoSocial.App.Utils.BitmapUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


public class Photo extends GeoNode implements Serializable {

	// Serializable UID
	private static final long serialVersionUID = -9121943636715457236L;
	
	private String mName;
	private String mDescription;
	private String mUrl;
	
	byte[]  mByteBitMapImageThumb, mByteBitMapImageMedium, mByteBitMapImageLarge, mByteBitMapImage;
	
	private User mUploader;
	
	public Photo (Integer id, Double latitude, Double longitude, Double altitude, Double radius, 
			      String name, String description, String url, String since, User uploader) 
	{
		super(id, latitude, longitude, altitude, radius, since);
		
		mName = name;
		mDescription = description;
		mUrl = url;
		
		mUploader = uploader;
		
		mByteBitMapImageThumb = null;
		mByteBitMapImageMedium = null;
		mByteBitMapImageLarge = null;
		mByteBitMapImage = null;



	}

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		mName = name;
	}

	public String getDescription() {
		return mDescription;
	}

	public void setDescription(String description) {
		mDescription = description;
	}

	public String getUrl() {
		return mUrl;
	}

	public void setUrl(String url) {
		mUrl = url;
	}

	public Bitmap getBitmapImageThumb() {		
		
		Bitmap bitmapImage = null;
		
		if (mByteBitMapImageThumb == null)
		{
			if (getId()==0)
				bitmapImage = BitmapUtils.loadBitmap(getExternalInfo().getPhotoThumbUrl());
			else
				bitmapImage = LibreGeoSocial.getInstance().getBitmap(mUrl + "thumb/");
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos))
			{
				Log.e("getBitmapImageThumb","Error: Don't compress de image");
				return null;
			}
			mByteBitMapImageThumb = baos.toByteArray();
			
		}
		
		return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImageThumb) );
		
	}
	
	public Bitmap getBitmapImageMedium() {		
		
		Bitmap bitmapImage = null;
		
		if (mByteBitMapImageMedium == null)
		{
			if (getId()==0)
				bitmapImage = BitmapUtils.loadBitmap(getExternalInfo().getPhotoNormalUrl());
			else
				bitmapImage = LibreGeoSocial.getInstance().getBitmap(mUrl + "medium/");
				
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos))
			{
				Log.e("getBitmapImageMedium","Error: Don't compress de image");
				return null;
			}
			mByteBitMapImageMedium = baos.toByteArray();
			
		}
		
		return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImageMedium) );
		
	}
	
	public Bitmap getBitmapImageLarge() {		
		
		if (mByteBitMapImageLarge == null)
		{
			Bitmap bitmapImage;
			
			if (getId()==0)
				bitmapImage = BitmapUtils.loadBitmap(getExternalInfo().getPhotoNormalUrl());
			else
				bitmapImage = LibreGeoSocial.getInstance().getBitmap(mUrl + "large/");
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos))
			{
				Log.e("getBitmapImageLarge","Error: Don't compress de image");
				return null;
			}
			mByteBitMapImageLarge = baos.toByteArray();
		}
		
		return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImageLarge) );
		
	}
	public Bitmap getBitmapImage() {		
	
	if (mByteBitMapImage == null)
	{
		Bitmap bitmapImage = LibreGeoSocial.getInstance().getBitmap(mUrl);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos))
		{
			Log.e("getBitmapImage","Error: Don't compress de image");
			return null;
		}
		mByteBitMapImage = baos.toByteArray();
	}
	
	return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImage) );
	
}

	public User getUploader() {
		return mUploader;
	}

	public void setUploader(User uploader) {
		mUploader = uploader;
	}

	public int describeContents() {
		return 0;
	}

	
	
}
